package vn.orderonline.com.core.business.menufood;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import vn.orderonline.com.core.mvc.model.MenuFoodDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.dao.FoodDao;
import vn.orderonline.core.dao.MenuFoodDao;
import vn.orderonline.core.dao.MenuFoodFoodDao;
import vn.orderonline.core.dao.RestaurantMenuFoodDao;
import vn.orderonline.core.dao.entity.MenuFood;
import vn.orderonline.core.dao.entity.MenuFoodFood;
import vn.orderonline.core.dao.entity.RestaurantMenuFood;
import vn.orderonline.core.dao.entity.RestaurantMenuFoodId;

@Service
@Transactional
public class MenuFoodImpl implements IMenuFood {

	@Autowired
	private MenuFoodDao menuFoodDao;
	@Autowired
	private RestaurantMenuFoodDao restaurantMenuFoodDao;
	@Autowired
	private MenuFoodFoodDao menuFoodFoodDao;
	@Autowired
	private FoodDao foodDao;
	
	@Override
	public List<MenuFoodDto> getListMenuFoodByRestaurantId(Integer resId) {
		MenuFoodDto dto = null;
		List<Object[]> list = menuFoodDao.getListMenuFoodByRestaurantId(resId);
		List<MenuFoodDto> listMenu = new ArrayList<>();
		for (Object[] obj : list) {
			dto = new MenuFoodDto();
			dto.setRestaurantId((Integer) obj[0]);
			dto.setRestaurantName((String) obj[1]);
			dto.setMenuFoodId((Integer) obj[2]);
			dto.setMenuFoodName((String) obj[3]);
			listMenu.add(dto);
		}
		return listMenu;
	}

	@Override
	public void saveMenuFood(MenuFoodDto menufood) {
		MenuFood menu = new MenuFood();
		menu.setMenuFoodName(menufood.getMenuFoodName());
		menu.setCreateTime(new Date(DateUtils.now()));
		menuFoodDao.save(menu);
		
		RestaurantMenuFood resMf = new RestaurantMenuFood();
		resMf.setRestaurantMenuFoodId(new RestaurantMenuFoodId(menufood.getRestaurantId(), menu.getMenuFoodId()));
		resMf.setCreateTime(new Date(DateUtils.now()));
		restaurantMenuFoodDao.save(resMf);
	}

	@Override
	public Boolean checkExistedMenuName(Integer resId, String menuName) {
		if (resId != null && !StringUtils.isEmpty(menuName)) {
			if(menuFoodDao.checkExistedMenuName(resId, menuName)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void deleteMenuFood(Integer restaurantId, Integer menuFoodId) {
		if (restaurantId != null && menuFoodId != null) {
			RestaurantMenuFood resMf = new RestaurantMenuFood();
			resMf = restaurantMenuFoodDao.find(new RestaurantMenuFoodId(restaurantId, menuFoodId));
			restaurantMenuFoodDao.delete(resMf);
			
			List<MenuFoodFood> listMff = menuFoodFoodDao.getListMenuFoodFoodByMenuFoodId(menuFoodId);
			for (MenuFoodFood mff : listMff) {
				menuFoodFoodDao.delete(mff);
				foodDao.delete(foodDao.find(mff.getId().getFoodId()));
			}
			
			MenuFood mf = new MenuFood();
			mf = menuFoodDao.find(menuFoodId);
			menuFoodDao.delete(mf);
		}
	}

	@Override
	public void updateMenuFood(Integer menuFoodId, String menuFoodName) {
		if (menuFoodId != null && !StringUtils.isEmpty(menuFoodName)) {
			MenuFood menuFood = menuFoodDao.find(menuFoodId);
			menuFood.setMenuFoodName(menuFoodName);
			menuFood.setUpdateTime(new Date(DateUtils.now()));
			menuFoodDao.update(menuFood);
		}
		
	}

}
